//
//  DetailStoryViewController.m
//  EDPS
//
//  Created by 张维扬 on 15/11/27.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "DetailStoryViewController.h"
#import "DetailStoryTableViewCell.h"
#import "ZWY_NetHandle.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "ExploreModel.h"
@interface DetailStoryViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSDictionary *userDic; // 用户信息字典
@property (nonatomic, retain) NSDictionary *textDic;//

@property (nonatomic, retain) NSMutableArray *detail_list_Arr;//下面数据
@end

@implementation DetailStoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGBA(253, 244, 233, 1);
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    // 请求数据
    [self getData];
    // 创建tableView
    [self createTableView];
    
}
#pragma mark - 网络请求
- (void)getData
{
    NSString *url = [NSString stringWithFormat: @"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@", self.idModel.spot_id.stringValue];
    [ZWY_NetHandle getURL:url Cookie:nil parameter:nil result:^(id result) {
        // 获取用户信息
        self.userDic = result[@"data"][@"trip"][@"user"];
        // 获取文本数据
        self.textDic = result[@"data"][@"spot"];
        // 下面详细数据
        self.detail_list_Arr = [ExploreModel transformWithArray:result[@"data"][@"spot"][@"detail_list"]];
        
        [self.tableView reloadData];
    }];
}
#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    self.tableView.backgroundColor = RGBA(253, 244, 233, 1);
    // 分区0的cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"detailCell_One"];
    // 分区1的cell
    [self.tableView registerClass:[DetailStoryTableViewCell class] forCellReuseIdentifier:@"detailCell_Two"];
    [self.view addSubview:self.tableView];
    
}
#pragma mark - 协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailCell_One"];
        cell.backgroundColor = RGBA(253, 244, 233, 1);
        cell.textLabel.text = self.textDic[@"text"];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.backgroundColor = RGBA(253, 244, 233, 1);
        return cell;
    }else{
        DetailStoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailCell_Two"];
        cell.model = self.detail_list_Arr[indexPath.row];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        // 计算文本高度
        // 设置一个字典 保存文本属性
        NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:20]};
        // 预设尺寸  文本最大不会超过这个尺寸
        CGSize size = CGSizeMake(375 - 20, 1000);
        // 根据文本信息  计算得到一个CGRect
        CGRect rect = [self.textDic[@"text"] boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        // 返回得到的高度
        return rect.size.height;
    }else{
        ExploreModel *tempModel = self.detail_list_Arr[indexPath.row];
        // 计算文本高度
        NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
        // 预设尺寸  文本最大不会超过这个尺寸
        CGSize size = CGSizeMake(375 - 20, 1000);
        CGRect rect = [tempModel.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        NSLog(@"%f", rect.size.height);
        // 返回得到的高度 +30 空隙高度
        return (tempModel.photo_height).integerValue * (ScreenWidth - 40) / (tempModel.photo_width).integerValue + rect.size.height + 30;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return self.detail_list_Arr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
#pragma mark - 分区头视图设置
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // viewframe设置多大都不影响 只取决于设置header高度
    UIView *userView = [[UIView alloc] initWithFrame:CGRectZero];
//    userView.backgroundColor = [UIColor redColor];
    // 用户头像
    UIImageView *headPic = [[UIImageView alloc] init];
    [userView addSubview:headPic];
    [headPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(userView.mas_centerY);
        make.left.mas_equalTo(userView.mas_left).offset(20);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    headPic.layer.cornerRadius = 15;
    headPic.clipsToBounds = YES;
    [headPic sd_setImageWithURL:[NSURL URLWithString:self.userDic[@"avatar_l"]] placeholderImage:[UIImage imageNamed:@"hold"]];
    // 用户名
    UILabel *name = [[UILabel alloc] init];
    [userView addSubview:name];
    name.text = self.userDic[@"name"];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(userView.mas_centerY);
        make.left.mas_equalTo(userView.mas_left).offset(60);
        make.size.mas_equalTo(CGSizeMake(ScreenWidth - 60, 30));
    }];
    
    UILabel *bottomLine = [[UILabel alloc] init];
    bottomLine.backgroundColor = [UIColor lightGrayColor];
    [userView addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(ScreenWidth - 70, 1));
        make.left.mas_equalTo(userView.mas_left).offset(35);
        make.bottom.mas_equalTo(userView.mas_bottom).offset(-1);
    }];
    
    if (section == 0) {
        return userView;
    }else{
        return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return ScreenWidth * 0.15;
    }else{
        return 0;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
